#include "2DDevice.h"
#include "Window.h"
#include "Select.h"
#include "QuadRenderer.h"
#include "LineRenderer.h"
#include "AstarPathFind.h"

#include "Map.h"
#include "Main.h"
#include "Vis.h"
#include "Path.h"



c2DDevice::c2DDevice(cMain & Main): Main(Main)
{
	g_pd3dDevice = NULL;
	g_pD3D = NULL;
	Restart();
}



c2DDevice::~c2DDevice()
{
	ReleaseInterface();
}

void c2DDevice::ReleaseInterface()
{
    
    if( g_pd3dDevice != NULL )
	{     
		g_pd3dDevice->Release();
		g_pd3dDevice = NULL;
	}
    if( g_pD3D != NULL )
	{
        g_pD3D->Release();
		g_pD3D = NULL;
	}
}

HRESULT c2DDevice::Restart()
{
	ReleaseInterface();

	    // Create the D3D object.
    if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
        return E_FAIL;

    // Set up the structure used to create the D3DDevice
    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory( &d3dpp, sizeof(d3dpp) );
    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;

    // Create the D3DDevice
    if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, Main.GetWindow().GetToolBoxHWND(),
                                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                      &d3dpp, &g_pd3dDevice ) ) )
    {
        return E_FAIL;
    }

    // Turn off culling, so we see the front and back of the triangle
    g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );

    // Turn off D3D lighting, since we are providing our own vertex colors
    g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );

   // D3DVIEWPORT9 viewData = { 0, 0, Window->GetClientAreaSizeWidth(), Window->GetClientAreaSizeHeight(), 0.0f, 1.0f };
//HRESULT hr;


//if(FAILED(g_pd3dDevice->SetViewport(&viewData)))
   // return E_FAIL;
	
		Main.GetMap().Restart();
	

	Main.GetSelect().Restart();

	return S_OK;
	
}


VOID c2DDevice::SetupMatrices()
{
    D3DXMATRIX matWorld;

	D3DXMatrixIdentity(&matWorld);

    g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );

    
    D3DXMATRIX matView;
    D3DXMatrixIdentity(&matView);
    g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );


    D3DXMATRIX matProj;
	D3DXMatrixOrthoLH(&matProj, Main.GetWindow().GetToolClientAreaSizeWidth(), Main.GetWindow().GetToolClientAreaSizeHeight(), -1, 1);

    g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
}


VOID c2DDevice::Render()
{
    // Clear the backbuffer to a black color
    g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0 );

    // Begin the scene
    if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
    {
        // Setup the world, view, and projection Matrices
        SetupMatrices();
	
        // Render the vertex buffer contents
	    Main.GetMap().Render();
		Main.GetMap().RenderNotWalkable();
		Main.GetAstarPathFind().Render();
		//QuadRenderer->AddQuadToRender(D3DXVECTOR3(0.0f,0.0f,0.0f), 0x00000000, QuadSize);
		
		//LineRenderer->AddLineToRender(D3DXVECTOR3(25.0f,-30.0f,0.0f), D3DXVECTOR3(400.0f,-30.0f,0.0f) , 0x0000FFFF, 1.0f);
		
		//if(AbstractGraphRenderer) AbstractGraphRenderer->Render();
		//HPAPathFind->Render(&Path);
		if((Main.GetState() == STATE_FINAL_PATH)||(Main.GetState() == STATE_HPAPATHFIND)||(Main.GetState() == SRC_AND_DEST_HPAPATHFIND)||(Main.GetState() == SRC_AND_DEST_HPAPATHFIND||(Main.GetState() == STATE_PATH_SMOOTHED)))
		{
			if(Main.GetSelect().GetSecondSelected())
			{
				Main.GetPath().RenderPath();
			}
		}
		
	Main.GetSelect().Render();
        // End the scene
	Main.GetVis().Render();
	Main.GetQuadRenderer().Render();
	Main.GetLineRenderer().Render();
	
        g_pd3dDevice->EndScene();
    }

    // Present the backbuffer contents to the display
    g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}


